package com.hktf.corelib.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.LruCache;

public class ImageLrucacheUtils {

	private static ImageLrucacheUtils instances;
	private LruCache<String, Bitmap> lruCache;
	private ExecutorService localThreadpool = Executors.newFixedThreadPool(1);

	private ImageLrucacheUtils() {
		int maxMemory = (int) ((Runtime.getRuntime().maxMemory()) / 1024);
		int lrucacheSize = maxMemory / 4;
		lruCache = new LruCache<String, Bitmap>(lrucacheSize) {
			@Override
			protected int sizeOf(String key, Bitmap value) {
				return value.getRowBytes() * value.getHeight() / 1024;
			}
		};
	}

	public static ImageLrucacheUtils getInstances() {

		if (instances == null) {
			instances = new ImageLrucacheUtils();
		}
		return instances;
	}

	public Bitmap loadNativeImage(final String key,final int width,final int height,final NativaBitmapCallBack callBack) {
		final Bitmap bitmap = getBitmapFromLrucache(key);
		final Handler hander = new Handler(){
			public void handleMessage(android.os.Message msg) {
				callBack.onImageLoad(key, (Bitmap)msg.obj);
			};
		};
		if (bitmap == null) {
			localThreadpool.execute(new Runnable() {
				
				@Override
				public void run() {
					Bitmap imageThumbnail = BitmapUtils.getImageThumbnail(key, width, height);
					Message msg = Message.obtain();
					msg.obj = imageThumbnail;
					hander.sendMessage(msg);
					putBitmapToLrucache(key, imageThumbnail);
				}
			});
		}
		return bitmap;

	}
	
	public Bitmap loadNativeShootImage(final String key,final int width,final int height,final NativaBitmapCallBack callBack) {
		final Bitmap bitmap = getBitmapFromLrucache(key);
		final Handler hander = new Handler(){
			public void handleMessage(android.os.Message msg) {
				callBack.onImageLoad(key, (Bitmap)msg.obj);
			};
		};
		if (bitmap == null) {
			localThreadpool.execute(new Runnable() {
				
				@Override
				public void run() {
					Bitmap imageThumbnail = BitmapUtils.getVideoThumbnail(key, width, height);
					Message msg = Message.obtain();
					msg.obj = imageThumbnail;
					hander.sendMessage(msg);
					putBitmapToLrucache(key, imageThumbnail);
				}
			});
		}
		return bitmap;

	}

	private Bitmap getBitmapFromLrucache(String key) {
		return lruCache.get(key);
	}

	private void putBitmapToLrucache(String key, Bitmap bitmap) {

		if (lruCache.get(key) == null && bitmap != null && key != null) {
			lruCache.put(key, bitmap);
		}
		LogMds.i("image", lruCache.size()+"");
	}
	
	public interface NativaBitmapCallBack{
		
		public void onImageLoad(String path,Bitmap bitmap);
	}
	
	public Bitmap loadNativeImage(final String key,final String path,final NativaBitmapCallBack callBack) {
		final Bitmap bitmap = getBitmapFromLrucache(key);
		final Handler hander = new Handler(){
			public void handleMessage(android.os.Message msg) {
				callBack.onImageLoad(key, (Bitmap)msg.obj);
			};
		};
		if (bitmap == null) {
			localThreadpool.execute(new Runnable() {
				
				@Override
				public void run() {
					Bitmap imageThumbnail =  BitmapFactory.decodeFile(path);
					//Bitmap imageThumbnail = BitmapUtils.getImageThumbnail(key, width, height);
					Message msg = Message.obtain();
					msg.obj = imageThumbnail;
					hander.sendMessage(msg);
					putBitmapToLrucache(key, imageThumbnail);
				}
			});
		}
		return bitmap;

	}


}
